#ifndef _SERVER_CORRESPOND_H
#define _SERVER_CORRESPOND_H

#include "stdio.h"
#include "ws2tcpip.h"

#include "server_log.h"
#include "server_assert.h"
#include "server_typedef.h"
#include "server_library_object.h"
#include "server_user.h"
#include "server_global_config.h"

#ifdef _WIN32
#include "winsock.h"
#endif
#ifdef __linux__
#include "sys/socket.h"
#endif

#include "server_typedef.h"

typedef enum _CorrespondDataType{
    CORRESPOND_DATA_TYPE_NULL = 0,
    CORRESPOND_DATA_TYPE_LOGIN,
    CORRESPOND_DATA_TYPE_LOGOUT,
    CORRESPOND_DATA_TYPE_REGISTER,
    CORRESPOND_DATA_TYPE_BORROW,
    CORRESPOND_DATA_TYPE_RETURN,
    CORRESPOND_DATA_TYPE_RENEW_BOOK,
    CORRESPOND_DATA_TYPE_QUERY_BOOK,
    CORRESPOND_DATA_TYPE_QUERY_BORROWED_BOOK,
    CORRESPOND_DATA_TYPE_QUERY_USER_INFO,
    CORRESPOND_DATA_TYPE_QUERY_USER_BORROWED_BOOK,
    CORRESPOND_DATA_TYPE_QUERY_USER_BORROWED_RECORD,
    CORRESPOND_DATA_TYPE_QUERY_USER_RENEW_RECORD,
    CORRESPOND_DATA_TYPE_QUERY_USER_RETURN_RECORD,
} CorrespondDataType;

// 登录结果枚举
typedef enum _CorrespondResult{
    // 注销结果
    LOGOUT_SUCCESS, // 注销成功
    LOGOUT_USER_NOT_LOGIN, // 用户未登录

    // 查询结果
    QUERY_SUCCESS, // 查询成功

    UNEXPECTED_ERROR, // 未知错误
} CorrespondResult;

typedef struct _CorrespondDataHead{
    uint32              session_id;     // 会话ID
    CorrespondDataType  type;           // 消息类型
    uint32              length;         // 数据长度
} CorrespondDataHead;

// 登录
typedef struct _CorrespondDataLogin{
    uint32      user_id;        // 用户ID
    wchar_t     password[16];   // 用户密码
} CorrespondDataLogin;

typedef struct _CorrespondDataLoginBack{
    UserFunctionReturn  result;     // 登录结果
    uint32              user_id;    // 用户ID
    wchar_t             name[16];   // 用户名
} CorrespondDataLoginBack;

// 注销
typedef struct _CorrespondDataLogout{
    uint32 user_id; // 用户ID
} CorrespondDataLogout;

typedef struct _CorrespondDataLogoutBack{
    CorrespondResult    result;     // 注销结果
    uint32              user_id;    // 用户ID
} CorrespondDataLogoutBack;

// 注册
typedef struct _CorrespondDataRegister{
    User user; // 用户信息
}CorrespondDataRegister;

typedef struct _CorrespondDataRegisterBack{
    uint8               result;     // 注册结果 0-成功 1-失败
    uint32              user_id;    // 用户ID
} CorrespondDataRegisterBack;

// 借书
typedef struct _CorrespondDataBorrow{
    uint32 object_id;
    uint16 count; // 借阅数量
}CorrespondDataBorrow;

typedef struct _CorrespondDataBorrowBack{
    ObjectFunctionReturn    result;     // 借书结果
} CorrespondDataBorrowBack;

// 还书
typedef struct _CorrespondDataReturn{
    uint32 object_id;
    uint16 count; // 归还数量
}CorrespondDataReturn;

typedef struct _CorrespondDataReturnBack{
    ObjectFunctionReturn    result;     // 还书结果
} CorrespondDataReturnBack;

// 续借
typedef struct _CorrespondDataRenew{
    uint16 record_list_number; // 续借哪一条记录
}CorrespondDataRenew;

typedef struct _CorrespondDataRenewBack{
    ObjectFunctionReturn    result;     // 续借结果
} CorrespondDataRenewBack;

// 查询图书
typedef struct _CorrespondDataQuery{
    wchar_t name[32]; // 图书名称
    StringIsSensitive is_sensitive; // 是否大小写敏感
} CorrespondDataQuery;

typedef struct _CorrespondDataQueryBack{
    uint8                   result;         // 查询结果 0-成功 1-失败
    LibraryObject           object[5];      // 返回前五条图书信息
} CorrespondDataQueryBack;

// 查询借阅记录
typedef struct _CorrespondDataQueryBorrowRecord{
    uint32 user_id; // 用户ID
} CorrespondDataQueryBorrowRecord;

typedef struct _CorrespondDataQueryBorrowRecordBack{
    uint8                   result;         // 查询结果
    UserLentObject          record[BOOK_MANAGE_CONFIG_MAX_BORROW_BOOKS_CONFIG];      // 返回8条借阅记录
} CorrespondDataQueryBorrowRecordBack;

extern SOCKET server_socket; // 服务器Socket

    uint8   server_correspond_init(void);
    uint8   server_correspond_close(void);
    SOCKET  server_correspond_accept_socket(void);
    void    server_correspond_event_handler(SOCKET client_socket, char* client_ip);

#endif
